import os
import sys

from molly.bo import File
from molly.util import logwrapper, QueueElement, QUEUE_ELEMENT, QUEUE_END


SEM_TIMEOUT = 3600


def scan(catalog, storage, dir_to_scan):
    assert storage.id is not None

    queue = Queue(100000)

    scan_process = Process(target=logwrapper, args=(_mp_scan, (queue, dir_to_scan), True))
    scan_process.start()

    insert_process = Process(target=logwrapper, args=(_mp_insert, (queue, catalog, storage), True))
    insert_process.start()

    scan_process.join()
    insert_process.join()

def _mp_scan(queue, dir_to_scan):
    for root, dirnames, filenames in os.walk(dir_to_scan):
        # add root
        queue.put(QueueElement(QUEUE_ELEMENT, File(root)))

        # add files
        for filename in filenames:
            file_path = os.path.join(root, filename)
            queue.put(QueueElement(QUEUE_ELEMENT, File(file_path)))

    queue.put(QueueElement(QUEUE_END, None))

def _mp_insert(queue, catalog, storage):
    file_dao = FileDao(catalog)

    while True:
        element = queue.get(timeout=SEM_TIMEOUT)

        if element.type == QUEUE_ELEMENT:
            file_obj = element.value
            file_dao.save(file_obj)

        elif element.type == QUEUE_END:
            break

